﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Media;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PPTHelper
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        readonly PPTHelper ppthelper = new PPTHelper();
        readonly Timer time = new Timer();
        bool Countdown
        {
            get => 启用ToolStripMenuItem.Checked;
            set
            {
                启用ToolStripMenuItem.Checked = value;
                声音提示ToolStripMenuItem.Enabled = value;
                结束放映ToolStripMenuItem.Enabled = value;
            }
        }
        bool Playsound
        {
            get => 声音提示ToolStripMenuItem.Checked;
            set => 声音提示ToolStripMenuItem.Checked = value;
        }
        bool EndshowPpt
        {
            get => 结束放映ToolStripMenuItem.Checked;
            set => 结束放映ToolStripMenuItem.Checked = value;
        }


        private void Form2_Load(object sender, EventArgs e)
        {
            this.Text = PPTHelper.Title;
            notifyIcon1.Text = this.Text;
            this.FormBorderStyle = FormBorderStyle.None;  //无边框
            this.ShowInTaskbar = false; //任务栏
            this.Opacity = 0.6;   //透明
            this.TopMost = true;  //顶层显示
            var rect = Screen.PrimaryScreen.Bounds;
            this.StartPosition = FormStartPosition.Manual;  //启动位置
            this.Location = new Point(rect.Width - 400, rect.Y);    //位置

            声音提示ToolStripMenuItem.Click += (o, ea) => Playsound = !Playsound;
            结束放映ToolStripMenuItem.Click += (o, ea) => EndshowPpt = !EndshowPpt;
            启用ToolStripMenuItem.Click += (o, ea) =>
             {
                 Countdown = !Countdown;
                 if (Countdown)
                 {
                     var form4 = new Form4cfg() { Location = MousePosition - new Size(30, 35) };
                     form4.ShowDialog();
                     ppthelper.CountdownTime = form4.TimeSpan;
                 }
                 else
                 {
                     ppthelper.CountdownTime = TimeSpan.Zero;
                 }
             };
            time.Interval = 1000;
            time.Tick += Time_Tick;
            time.Start();
        }

        private void Time_Tick(object sender, EventArgs e)
        {
            var t = ppthelper.Time.Ticks;
            if (t != 0) label1.Text = ppthelper.TimeText;
            if (ppthelper.Time > new TimeSpan(0, 0, 0) && ppthelper.Time < new TimeSpan(0, 0, 1))
            {
                var file = "Sound.wav";
                if (Playsound && File.Exists(file)) new SoundPlayer(file).Play();
                if (EndshowPpt) ppthelper.Stop();
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var version = Assembly.GetExecutingAssembly().GetName().Version;
            var title = $"{PPTHelper.Title} {version.Major}.{version.Minor}";
            var url = "https://gitee.com/zhux3/ppthelper";
            new Form3(url, title).Show();
        }

        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            Win32Api.Control_MouseDown(this.Handle);
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            try
            {
                var ipAddr = Dns.GetHostAddresses(Dns.GetHostName());
                var ips = ipAddr.Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    .Select(i => $"http://{i}:{HttpServer.Port}");
                远程摇控ToolStripMenuItem.DropDownItems.Clear();
                foreach (var ip in ips)
                {
                    var item = 远程摇控ToolStripMenuItem.DropDownItems.Add(ip);
                    item.Click += Item_Click;
                }
            }
            catch (Exception ex)
            {
                Log.Warn(ex.Message);
            }
        }

        private void Item_Click(object sender, EventArgs e)
        {
            var obj = sender as ToolStripMenuItem;
            var form3 = new Form3(obj.Text);
            form3.Show();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
            this.Activate();
        }

        private void 最小化ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            this.Activate();
        }

        private void 开始播放ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ppthelper.Play();
        }

        private void 上一步ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ppthelper.Previous();
        }

        private void 下一步ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ppthelper.Next();
        }

        private void 最后一页ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ppthelper.Last();
        }

        private void 第一页ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ppthelper.First();
        }

        private void 退出播放ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ppthelper.Stop();
        }

        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            ppthelper.Dispose();
        }

    }
}
